/**
 * 
 */
package com.atung.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.atung.dao.BusOrderRefundDAO;
import com.atung.dao.BusOrederCarLCDAO;
import com.atung.exceptiom.LcExceptionEnum;
import com.atung.model.BusCarRide;
import com.atung.model.BusOrderRefund;
import com.atung.model.RefundDO;
import com.atung.request.RequestRefund;
import com.atung.service.WXPayService;
import com.yuyi.full.Result;


/**
 * 
 * @author lc <br>
 * 2020年11月17日 下午2:59:11
 */
@Service
public class WXPayServiceImpl implements WXPayService {
    
    @Autowired
    private BusOrederCarLCDAO busOrederCarLCDAO;
    
    @Autowired
    private BusOrderRefundDAO busOrderRefundDAO; 
    
    @Override
    @Transactional
    public Result<?> refund(RequestRefund red) throws Exception {
        //查询退款表
        BusOrderRefund byOrderId = busOrderRefundDAO.getByOrderId(red.getOrderId());
        if (byOrderId !=null) {
            return Result.error(LcExceptionEnum.ORDER_STATE_ERRO);
        }
        //获取订单信息。。。。。
        RefundDO order = busOrederCarLCDAO.getById(red.getOrderId(),red.getOpenId());
        if (order == null) {
            return Result.error(LcExceptionEnum.ORDER_ERRO);
        }
        //判断订单信息是否为已支付订单
        if (order.getOrderStatus() != 202) {
           return Result.error(LcExceptionEnum.ORDER_STATE_ERRO);
        }
        if (order.getOrderState()!= 300 &&order.getOrderState()!= 301) {
            return Result.error(LcExceptionEnum.TRAVLE_REFUND_ERRO);
        }
        
//        如果为已支付订单 判断线路是否超出规定时间  如果已经超出规定时间  判断退款金额
//        如果为已支付订单 判断是否安排车辆  判断退款金额
        BigDecimal money = order.getOrderPayableMoney();
        BigDecimal totalFee = money.multiply(new BigDecimal(100.00));
        BigDecimal refundFee = totalFee;
        LocalDateTime expirationTime = busOrederCarLCDAO.getRouteExpirationTime(order.getRoutPublishId());        
        LocalDateTime refund = LocalDateTime.now();
        Long allocId = order.getAllocationId();
        Long carId = busOrederCarLCDAO.getCarIdById(allocId);
        if (carId !=null) {
            refundFee = totalFee.divide(new BigDecimal(2));
        }
        //添加退款表
        BusOrderRefund busOrderRefund = new BusOrderRefund();
        busOrderRefund.setOrderId(red.getOrderId());
        busOrderRefund.setOpenId(red.getOpenId());
        busOrderRefund.setRefundFee(refundFee.divide(new BigDecimal(100)));
        busOrderRefund.setTotalFee(money);
        busOrderRefund.setRefundState(400);
        busOrderRefund.setRefundDesc(red.getRefundDesc());

        busOrderRefund.setRefundTime(refund);
        busOrderRefund.setRoutExpirationTime(expirationTime);
        busOrderRefund.setRefundExplain(red.getRefundExplain());
        busOrderRefund.setCreationTime(refund);
//        System.out.println(busOrderRefund);
        busOrderRefundDAO.insert(busOrderRefund);
        //修改订单状态为退款中
        busOrederCarLCDAO.updateOrderRefund(order.getId(),refund);
        busOrederCarLCDAO.updatePublishRefund(order.getPaymentId(),refund);
        //修改车辆分配表人员信息
        List<BusCarRide> listRide = busOrederCarLCDAO.getListRideByOrderId(order.getId());
        if (listRide!=null && !listRide.isEmpty()) {
            
            Long allocationId = listRide.get(0).getAllocationId();
            String nember = busOrederCarLCDAO.getNemberById(allocationId);
            Integer nowNumber= Integer.parseInt(nember)-listRide.size();
            //修改车辆乘坐人员
            busOrederCarLCDAO.updateNemberById(allocationId, String.valueOf(nowNumber),refund);
            //修改车辆分配人员状态   删除
            busOrederCarLCDAO.updateDeletByOrderId(order.getId(),refund);
        }
        return Result.success();
    }
    
   

}
